<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleSave"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '详情',
                icon: 'clarity:info-standard-line',
                onClick: handleDesc.bind(null, record),
              },
              {
                label: '编辑',
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '管理',
                icon: 'clarity:note-edit-line',
                onClick: handleManage.bind(null, record),
              },
              {
                label: '删除',
                color: 'error',
                icon: 'ant-design:delete-outlined',
                popConfirm: {
                  title: '确认删除？',
                  okText: '确认',
                  cancelText: '取消',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <GroupDesc @register="registerDescModal" />
    <GroupEdit @register="registerEditModal" />
  </div>
</template>
<script lang="ts" setup>
  import GroupDesc from "./Desc.vue";
  import GroupEdit from "./Edit.vue";
  import { useModal } from "/@/components/Modal";
  import { useMessage } from "/@/hooks/web/useMessage";
  import { defaultActionColumn, defaultTableSettings } from "/@/hooks/core/useDefault";
  import { BasicTable, TableAction, useTable } from "/@/components/Table";
  import { useRouter } from "vue-router";
  import { getGroupPageApi, removeGroupApi } from "/@/api/core/group";
  import { groupBasicColumns } from "/@/views/default/data";
  import { OperTypeEnum } from "/@/enums/typeEnum";

  const [registerTable] = useTable({
    title: "小组列表",
    rowKey: "groupId",
    columns: groupBasicColumns,
    api: getGroupPageApi,
    actionColumn: {
      width: 320,
      ...defaultActionColumn,
    },
    ...defaultTableSettings,
  });

  const [registerDescModal, { openModal: openDescModal }] = useModal();
  const [registerEditModal, { openModal: openEditModal }] = useModal();

  function handleDesc(data) {
    openDescModal(true, data);
  }

  function handleEdit(data) {
    openEditModal(true, { data, operType: OperTypeEnum.UPDATE });
  }

  function handleSave() {
    openEditModal(true, { operType: OperTypeEnum.SAVE });
  }

  const router = useRouter();
  function handleManage(record) {
    router.push({
      name: "Manage",
      query: { groupId: record.groupId },
    });
  }

  const { createMessage } = useMessage();
  async function handleDelete(data) {
    await removeGroupApi(data.groupId);
    createMessage.info("删除小组成功");
  }
</script>
